//actions可以处理异步请求的
import { getSeckillApi, getHomeListApi, getHomeCateListApi, getHomeGoodsListApi, getCategoryLeftListApi, getCategoryRightListApi, getGoodsDetailApi } from "@/api/index.js";
// console.log(getSeckillApi);
let actions = {
    //提交mutations中的方法来修改state
    async actChangeSeckillListDatas(context, params) {
        let result = await getSeckillApi(params)
        // console.log(result.data);
        // context.commit("mutations中定义的方法", ajax请求出来的数据)
        context.commit("changeSeckillListDatas", result.data)
    },
    async actChangeHomeListDatas(context, params) {
        let result = await getHomeListApi(params)
        // console.log(result.data);
        context.commit("changeHomeListDatas", result.data)
    },
    async actChangeHomeCateListDatas(context, params) {
        let result = await getHomeCateListApi(params)

        context.commit("changeHomeCateListDatas", result.data)
    },
    //获取产品列表
    async actChangeHomeCoodsListDatas(context, params) {
        context.commit("changeIsLoading", true)  //发起请求显示loading图标
        let result = await getHomeGoodsListApi(params)
        // console.log(result.data);
        if (result.data.length == 0) {
            //如果条件成立说明没有更多数据了
            context.commit("changeIsLoading", false)
        }
        context.commit("changeHomeCoodsListDatas", result.data)
    },
    //分类左侧数据
    async actChangeCategoryLeftDatas(context, cat_id) {
        let result = await getCategoryLeftListApi()
        // console.log(result.data);
        let adData = result.data.filter(item => {
            return item.cat_id == cat_id;
        });
        context.commit("changeCateAd", adData[0].touch_catads)
        // context.commit("mutations中定义的方法", result.data)
        context.commit("changeCategoryLeftDatas", result.data)

    },
    //分类右侧数据
    async actChangeCategoryRightDatas(context, cat_id) {
        let result = await getCategoryRightListApi(cat_id)
        // console.log(result.data);
        // context.commit("mutations中定义的方法", result.data)
        context.commit("changeCategoryRightDatas", result.data)
    },
    //商品详情数据 
    async actChangeGoodsDetailDatas(context, params) {
        let result = await getGoodsDetailApi(params)
        console.log(result.data);

        let goods_thumb = result.data.goods_thumb
        let goods_name = result.data.goods_name
        let shop_price = result.data.shop_price
        console.log(goods_thumb);
        console.log(goods_name);
        console.log(shop_price);

        //转换base64

        function base64(img) {
            //创建一个canvas画布

            var canvas = document.createElement("canvas")  //<canvas/>
            var ctx = canvas.getContext("2d")
            console.log(ctx);

            canvas.width = img.width  //给canvas画布设置大小
            canvas.height = img.height
            img.crossOrigin = 'anonymous';//解决图片跨域问题
            let index = img.src.lastIndexOf(".") + 1
            console.log(img.src.slice(index));
            let ext = img.src.slice(index).toLowerCase()  //找到图片的后缀
            ctx.drawImage(img, 0, 0, img.width, img.height)
            var dataUrl = canvas.toDataURL("image/" + ext)
            console.log();
            return dataUrl

        }

        var image = new Image(); // 
        // console.log(image); <img/>
        image.src = goods_thumb


        image.onload = function () {
            var base64img = base64(image)
            var posterDatas = {
                goods_thumb: base64img,
                goods_name: goods_name,
                shop_price: shop_price
            }
            context.commit("changePosterDatas", posterDatas)
        }

        context.commit("changeGoodsDetailDatas", result.data)
    }
}
export default actions